package com.mai.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.mai.entity.UserMai;
import com.mai.service.UserMaiService;
import com.mai.util.MD5Util;

@Controller
@RequestMapping("user")
@ResponseBody
@SessionAttributes("user")
public class UserMaiController {
	@Autowired
	private UserMaiService ums;
	
	/**
	 *	登录
	 * @return
	 */
	@RequestMapping("/login")
	public String login(String loginName, String loginPassword, Model model, HttpServletRequest request, HttpServletResponse response) {
		UserMai user = ums.selectUserByName(loginName);
		if(user != null) {
			String password = user.getPassword();
			if(MD5Util.Decrypt(password, loginPassword)) {
				model.addAttribute("user", user);
				if(user.getUser_power().equals("管理员")){
					return "loginroot";
				}else {
					return "loginsucess";
				}			
			}else {
				System.out.println("密码错误");
				return "loginfail";
			}
		}else {		
			System.out.println("用户不存在");
			return "loginNull";
		}
	}
	
	/**
	 *	注册 
	 * @return
	 */
	@RequestMapping("/register")
	public String register(String registerName, String registerPassword, int loginNum) {
		UserMai user = ums.selectUserByName(registerName);
		if(user != null) {
			return "registerfail";
		}else {
			String pwd = MD5Util.encryption(registerPassword);
			UserMai u = new UserMai(registerName, pwd, loginNum);
			ums.addUser(u);
			return "registersucess";
		}
	}
	
	/**
	 * 修改密码
	 */
	@ResponseBody
	@RequestMapping("/upPwd")
	public String upPwd(String userName, String opwd, String ypwd) {
		System.out.println("用户名"+userName+"旧密码："+opwd+"  新密码"+ypwd);
		UserMai user = ums.selectUserByName(userName);
		if(user != null) {
			if(MD5Util.Decrypt(user.getPassword(), opwd )) {
				String ypwd1 = MD5Util.encryption(ypwd);
				user.setPassword(ypwd1);
				System.out.println(user);
				ums.resetPassword(user);
				return "0";
			}else {
				return "1";
			}
		}else {
			return "1";
		}
	}
	
	/**
	 *	 重置密码
	 * @return
	 */
	@RequestMapping("/resetPassword")
	public String resetPassword(String resetName, String resetPassword, int loginNum) {
		UserMai user = ums.selectUserByName(resetName);
		if(user == null) {
			return "resetfail";
		}else {
			if(user.getLoginNum().equals(loginNum)) {
				String pwd = MD5Util.encryption(resetPassword);
				UserMai u = new UserMai(resetName, pwd, loginNum);
				ums.resetPassword(u);
				return "resetsucess";
			}else {
				return "loginNumfail";
			}
		}
	}
	
}
